{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7.12 Orifice Size for Given Pressure Drop and Velocity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A 12\" schedule 40 steel pipe is 60 feet long; it serves\n",
    "to connect water from a reservoir to the atmosphere.\n",
    "The water is 60 deg F and the elevation distance of the pipe\n",
    "is 12 feet.\n",
    "\n",
    "The pipe also contains a gate valve 10' from the entrance. The entrance into the reservoir is at a distance from the wall.\n",
    "\n",
    "Find the diameter of a standard orifice plate to make the flow velocity exactly 10 ft/s.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numerical solver progress:\n",
      "131456.14904692862 0.5 0.6039115256189872\n",
      "131207.12017516745 0.50015 0.6039153509057876\n",
      "45473.52362140946 0.5791812700976602 0.6054825503405404\n",
      "22370.19792138349 0.6210577694292391 0.6055902934716847\n",
      "7540.056806983848 0.6616054200344094 0.6047579920946973\n",
      "1943.8664434596685 0.6822209749771645 0.6038212679012359\n",
      "240.43098826401547 0.6893818974732472 0.6033941581381093\n",
      "9.046744122446398 0.6903926241593472 0.6033292370702888\n",
      "Size is:\n",
      "8.241150267140824 inch\n"
     ]
    }
   ],
   "source": [
    "from fluids.units import *\n",
    "from fluids.constants import g\n",
    "from math import pi\n",
    "from scipy.optimize import newton\n",
    "g = g*u.m/u.s**2\n",
    "\n",
    "dH = 12*u.foot\n",
    "L = (60)*u.foot\n",
    "mu = 1.1*u.cP\n",
    "rho = 62.364*u.lb/u.ft**3\n",
    "\n",
    "NPS, Di, Do, t = nearest_pipe(Do=12*u.inch, schedule='40')\n",
    "\n",
    "v = 10*u.foot/u.s\n",
    "A = 0.25*pi*Di**2\n",
    "m = v*A*rho\n",
    "Re = rho*v*Di/mu\n",
    "\n",
    "# Approximately match their friction factor, roughness not specified\n",
    "fd = friction_factor(Re=Re, eD=0.0006*u.inch/Di)\n",
    "ft = ft_Crane(Di) # Not needed\n",
    "\n",
    "K_entrance = entrance_distance(Di=Di, method='Crane')\n",
    "K_exit = exit_normal()\n",
    "K_gate = K_gate_valve_Crane(D1=Di, D2=Di, angle=0.0*u.degrees)\n",
    "\n",
    "K_tot = K_entrance + K_exit + K_gate\n",
    "K_tot += K_from_f(fd=fd, L=L, D=Di)\n",
    "\n",
    "dP = dP_from_K(K=K_tot, rho=rho, V=v)\n",
    "k = 1.33\n",
    "\n",
    "dP_gravity = rho*g*dH\n",
    "\n",
    "dP_meter =  dP_gravity - dP\n",
    "meter_type = 'ISO 5167 orifice'\n",
    "orifice_taps = 'corner'\n",
    "\n",
    "# P2 is assumed - does have an impact but it is minimal\n",
    "P2 = 1e6*u.Pa\n",
    "P1 = (P2 + dP_meter*2.5)\n",
    "print('Numerical solver progress:')\n",
    "def to_solve(beta):\n",
    "    # Do not know orifice measured dP or C, only actual dP  \n",
    "    D2 = Di*beta\n",
    "    \n",
    "    # Solve for upstream pressure given actual flow rate and guessed diameter\n",
    "    P1 = differential_pressure_meter_solver(Di, rho, mu, k, D2=D2, P2=P2, \n",
    "                                       m=m, meter_type=meter_type, \n",
    "                                       taps=orifice_taps)\n",
    "    \n",
    "    # Calculate `C`\n",
    "    C, expansibility = differential_pressure_meter_C_epsilon(Di, D2, m, P1, P2, rho, mu, k, \n",
    "                                              meter_type=meter_type, taps=orifice_taps)\n",
    "    \n",
    "    # Caclulate what the guessed orifice actual dP is\n",
    "    calc_dP = dP_orifice(D=Di, Do=D2, P1=P1, P2=P2, C=C)\n",
    "    err = (calc_dP - dP_meter).to_base_units()\n",
    "    print(err.magnitude, beta, C.magnitude)\n",
    "    return err.magnitude\n",
    "beta = newton(to_solve, .5, tol=1e-4)\n",
    "print('Size is:')\n",
    "print((Di*beta).to(u.inch))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The answer given in Crane is 7.94 inches, but only three iterations are performed there."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
